package com.yanqu.road.server.manger.config;

import com.yanqu.road.entity.enums.eChannelType;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.logic.bussiness.servercenter.ServerBussiness;
import com.yanqu.road.pb.common.CommonProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.activity.curio.CurioConfigMgr;
import com.yanqu.road.server.manger.activity.curio.CurioMgr;
import com.yanqu.road.server.manger.plugin.ManagerReloadListener;
import com.yanqu.road.server.protocol.ClientProtocol;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author wpj
 * @Date 2020/12/24
 */
public class HideGoodsMgr extends TempMgr {

    private static List<Integer> hideGoods;

    private static List<ManagerReloadListener> managerReloadListener = new ArrayList<>();

    private static List<Integer> hideGoodsReview;

    @Override
    public boolean reloadData() throws Exception {
        int channelId = ServerListMgr.getServerInfo(GameServer.getInstance().getServerId()).getChannelId();
        hideGoods = ServerBussiness.getHideGoodsId(channelId);
        hideGoodsReview = ServerBussiness.getHideGoodsIdReview(channelId);
        notifyReload();
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean init() throws Exception {
        boolean reload = reload();
        addReloadListener(() -> curioListener());
        return reload;
    }

    private static void notifyReload(){
        if(null != managerReloadListener){
            for (ManagerReloadListener listener : managerReloadListener) {
                listener.notifyReload();
            }
        }
    }

    public static void addReloadListener(ManagerReloadListener reloadListener){
        managerReloadListener.add(reloadListener);
    }

    private static void curioListener() {
        CurioConfigMgr.reloadCurioConfig();
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void syncHideGoods(GamePlayer player) {
        CommonProto.HideGoodsMsg.Builder builder = CommonProto.HideGoodsMsg.newBuilder();
        for (int goodId : getHideGoods()) {
            builder.addGoodId(goodId);
        }
        player.sendPacket(ClientProtocol.U_HIDE_GOODS, builder);
    }

    public static List<Integer> getHideGoods() {
        int channelId = ServerListMgr.getServerInfo(GameServer.getInstance().getServerId()).getChannelId();
        if(eChannelType.isMainLandChannel(channelId)){
            ServerInfo serverInfo = ServerListMgr.getServerInfo(GameServer.getInstance().getServerId());
            if(null != serverInfo && serverInfo.getServerType() == ServerListMgr.SERVER_TYPE_REVIEW) {//提审服用提审服的配置
                return hideGoodsReview;
            }else {
                return hideGoods;
            }
        }else {
            return hideGoods;
        }
    }

    /**
     * 家具是否屏蔽 不在列表是屏蔽
     * @param furnitureId
     * @return
     */
    public static boolean isFurnitureHide(int furnitureId) {
        return !getHideGoods().contains(furnitureId);
    }



}
